package com.fallsvc.dayproblem.demo202511;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/13  15:09
 */
public class Demo20251113 {
    public static void main1(String[] args) {

    }

    //    https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/
    // 删除有序数组的重复项
    public int removeDuplicates1(int[] nums) {
        int l = 0;
        int r = 0;
        int count = 1;// 算上第一个数字了
        while (r + 1 < nums.length) {
            if (nums[r] == nums[r + 1]) r++;
            else {
                r++;// 找到向前添加元素
                nums[++l] = nums[r];
                count++;
            }
        }
        return count;
    }

    //    https://leetcode.cn/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/submissions/677870404/?envType=problem-list-v2&envId=recursion

    public int iceBreakingGame1(int num, int target) {
        int x = 0;
        for (int i = 2; i != num + 1; i++) {
            x = (target + x) % i;// num 为i时 返回值
        }
        return x;

    }
    // 递归
    public int iceBreakingGame2(int num, int target) {

        return numForValue(num,target);
    }
    public int numForValue(int num,int target) {
        if(num == 1) return 0;
        // 求num - 1 返回位置
        int x = numForValue(num - 1,target);
        return (target + x) % num;
    }
    //
    // https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/?envType=study-plan-v2&envId=top-interview-150
    // 删除有序数组的重复项2
    public int removeDuplicates(int[] nums) {
        int len = nums.length;
        int l = 1;

        if(l < len && nums[0] == nums[1]) l += 1;
        int count = l;
        int r = 1;

        while(r < len) {
            if(nums[r] != nums[r - 1]) {   // 找到需要向前移动的数
                if(r + 1 < len && nums[r + 1] == nums[r]) {
                    // 多移1位
                    nums[l++] = nums[r++];
                    count++;
                }
                count++;
                nums[l++] = nums[r++];
            }else {
                r++;
            }
        }
        return count;
    }

    public int removeDuplicates2(int[] nums) {
        int n = nums.length;
        if (n <= 2) {
            return n;
        }
        int slow = 2, fast = 2;
        while (fast < n) {
            if (nums[slow - 2] != nums[fast]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }


}
